package com.techpleiad.ieducation.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.techpleiad.ieducation.dao.ILoginDao;
import com.techpleiad.ieducation.entity.UserAccount;
import com.techpleiad.ieducation.exception.GenericDaoException;

@Repository("loginDao")
@Transactional
public class LoginDaoImpl extends AbstractDaoSupport<UserAccount, Long> implements ILoginDao {

	public LoginDaoImpl() {
		setClazz(UserAccount.class);
	}
	
	@SuppressWarnings("unchecked")
	public UserAccount get(final String username, final String password)
			throws GenericDaoException {
		UserAccount userAccount = null;
		try {
			DetachedCriteria dc = DetachedCriteria.forClass(UserAccount.class);
			dc.add(Restrictions.eq("qualifiedUserName", username));
			dc.add(Restrictions.eq("password", password));
			
			List<UserAccount> userAccounts = this.findByCriteria(dc);
			if (!userAccounts.isEmpty()) {
				userAccount = userAccounts.get(0);
			}
		} catch (DataAccessException dae) {
			throw new GenericDaoException(dae);
		}
		return userAccount;
	}
}
